package de.lmu.ifi.pst.uwejsf.framework;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;

public class OGNLEvaluatedPropertyHandler implements EvaluatedPropertyHandler {

	private String expression;
	
	@Override
	public Object evaluate(Object context) {
		if (expression == null)
			return null;
		AbstractUWEContextFactory contextFactory = AbstractUWEContextFactory
				.getCurrentInstance();
		ScriptEngine engine = contextFactory.getScriptEngineByName("ognl");
		ScriptContext scriptContext = contextFactory.createScriptContext(context);
		
		Object result = null;
		try {
			result = engine.eval(expression, scriptContext);
		} catch (Exception e) {
			throw new NavigationException("Couldn't evaluate OGNL script: "
					+ expression, e);
		}
		return result;
	}

	@Override
	public String getExpression() {
		return expression;
	}

	@Override
	public void setExpression(String expression) {
		this.expression = expression;
		
	}

}
